//app.js
App({
  util: require('we7/resource/js/util.js'),
  siteInfo: require('siteinfo.js'),
  onLaunch: function () {
    // 展示本地存储能力
  },
  getUserInfo:function(userInfo){
    var that = this
    // 登录
    wx.login({
      success: function (res) {
        var code = res.code
        that.util.request({
          'url': 'entry/wxapp/openid',
          'cachetime': '0',
          data: { code: code },
          header: {
            'content-type': 'application/json'
          },
          dataType: 'json',
          success: function (res) {
            userInfo(res.data.openid)
            getApp().globalData.openid = res.data.openid;
						wx.setStorageSync('openid', res.data.openid)
          },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
  },
  // menuset:function (menuset){
  //   var that=this
  //   that.util.request({
  //     'url': 'entry/wxapp/menuset',
  //     success: function (res) {
  //       console.log(res.data)
  //       getApp().globalData.menuset = res.data
  //       wx.setStorageSync('menuset', res.data)
  //     }
  //   });
  // },

  url:function (siteUrl){
    var that=this
    that.util.request({
      'url': 'entry/wxapp/siteurl',
      'cachetime': '0',
      success: function (res) {
        getApp().globalData.siteUrl = res.data
				wx.setStorageSync('siteUrl', res.data)
      }

    });
  },

  uniacid: function (uniacid) {
    var that = this
    that.util.request({
      'url': 'entry/wxapp/uniacid',
      'cachetime': '0',
      success: function (res) {
        getApp().globalData.uniacid = res.data

				wx.setStorageSync('uniacid', res.data)
      }

    });
	},

	attachurl: function (attachurl) {
		var that = this
		that.util.request({
			'url': 'entry/wxapp/attachurl',
			'cachetime': '0',
			success: function (res) {
				getApp().globalData.attachurl = res.data

				wx.setStorageSync('attachurl', res.data)
			}

		});
	},
 
  location_url: function (n) {
    var url = this.globalData.url;
    n.setData({
      url: url
    })
    var e = this;
    url || e.util.request({
      'url': 'entry/wxapp/siteurl',
      success: function (res) {
        e.globalData.url = res.data
        e.location_url(n)
      }
    })
  },
  location_uniacid: function (n) {
    var uniacid = this.globalData.uniacid;
    n.setData({
      uniacid: uniacid
    })
    var e = this;
    uniacid || e.util.request({
      'url': 'entry/wxapp/uniacid',
      success: function (res) {
        e.globalData.uniacid = res.data
        e.location_uniacid(n)
      }
    })
  },
  location_attachurl: function (n) {
    var attachurl = this.globalData.attachurl;
    n.setData({
      attachurl: attachurl
    })
    var e = this;
    attachurl || e.util.request({
      'url': 'entry/wxapp/attachurl',
      success: function (res) {
        e.globalData.attachurl = res.data
        e.location_attachurl(n)
      }
    })
  },
  route: function (e) {
    var that = this
		that.util.request({
			'url': 'entry/wxapp/menuset',
			success: function (res) {
				console.log(res.data)
				getApp().globalData.menuset = res.data
				console.log(getApp().globalData.menuset)
				wx.setStorageSync('menuset', res.data)
				console.log(wx.getStorageSync('menuset'))
			}
		});
    var obj = wx.getStorageSync('tabbar')
    var url1 = wx.getStorageSync('url')
    function ifArrVal(arr, value) {//多维数组判断是否存在某值
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] instanceof Array) {//判断是否为多维数组
          return ifArrVal(arr[i].urls, value);
        } else {
          if (arr[i].urls == value) {
            return 1;//存在
          }
        }
      }
      return -1;//不存在
    }
    function a(t) {
      var a = !1
      var o = e.route || e.__route__ || null
      for (let i in t) {
        var url = "zh_gjw/pages" + t[i].url.slice(2, t[i].url.length)
        t[i].urls = "zh_gjw/pages" + t[i].url.slice(2, t[i].url.length)
        if (obj[i] != null) {
          t[i].color = obj[i].color2
          t[i].url = obj[i].url
          t[i].sele_color = obj[i].color1
          t[i].icon = url1 + '' + obj[i].icon2
          t[i].urls = "zh_gjw/pages" + obj[i].url.slice(2, obj[i].url.length)
          t[i].name = obj[i].title
          t[i].sele_icon = url1 + '' + obj[i].icon1
        }
        if (t[i].urls == o) {
          t[i].active = true
        } else {
          t[i].active = false
        }
      }
      var arr = ifArrVal(t, o)
      if (arr == 1) {
        e.setData({
          sele: true,
          tabbar: t
        })
      } else {
        e.setData({
          sele: false,
          tabbar: t
        })
      }
    }
		function menus(m){
			console.log(m)
			var menus = wx.getStorageSync('menuset')
			var routess = m
			var menuindex = [{ index: menus.db }, { index: menus.wp }, { index: menus.dt }, { index: menus.sc }, { index: menus.dz }, { index: 0 }]
			for (var i = 0; i < menuindex.length; i++){
				if(menuindex[i].index==1){
					delete routess[i]
					// menuindex.splice(i, 1)
					// routess.splice(i, 1)
				}
			}
			var result = [];
			for (var i = 0; i < routess.length; i++) {
				if (typeof (routess[i]) != 'undefined') {
					result.push(routess[i]);
				}
			}
			console.log(menuindex)
			console.log(routess)
			console.log(result)
			return result
		}
    var routes = [
      {
        name: '抖宝',
        color: '#5d5d5d',
        active: true,
        url: '../index/index',
        sele_color: '#35B1F7',
        icon: '../images/01_ico.png',
        sele_icon: '../images/01_on_ico.png',
      },
      {
        name: '微拍',
        color: '#5d5d5d',
        active: false,
        url: '../weipai/paimai',
        sele_color: '#35B1F7',
        icon: '../images/02_ico.png',
        sele_icon: '../images/02_on_ico.png',
      },
      {
        name: '地摊',
        color: '#5d5d5d',
        active: false,
        sele_color: '#35B1F7',
        url: '../jishi/youxuan',
        icon: '../images/03_ico.png',
        sele_icon: '../images/03_on_ico.png',
      },
      {
        name: '商城',
        color: '#5d5d5d',
        active: false,
        sele_color: '#35B1F7',
        url: '../fixPrice/youxuan',
        icon: '../images/03_ico.png',
        sele_icon: '../images/03_on_ico.png',
      },
      {
        name: '定制',
        color: '#5d5d5d',
        active: false,
        url: '../dingzhi/store',
        sele_color: '#35B1F7',
        icon: '../images/04_ico.png',
        sele_icon: '../images/04_on_ico.png',
      },
      {
        name: '我的',
        color: '#5d5d5d',
        url: '../member/index',
        active: false,
        sele_color: '#35B1F7',
        icon: '../images/05_ico.png',
        sele_icon: '../images/05_on_ico.png',
      },
    ]
		a(menus(routes))
  },
  globalData: {
    userInfo: null,
    openid:'',
    siteUrl:'',
    uniacid:'',
		attachurl:'',
    avatarUrl:''
  }
})